package cn.restcloud.example.controller;

import java.util.List;

import org.bson.Document;

import cn.restcloud.example.dao.IDemoUserDao;
import cn.restcloud.example.dao.impl.DemoUserRdbImpl;
import cn.restcloud.example.model.UserModel;
import cn.restcloud.framework.core.annotation.bean.BeanConfig;
import cn.restcloud.framework.core.annotation.bean.BeanType;
import cn.restcloud.framework.core.annotation.rest.GetParam;
import cn.restcloud.framework.core.annotation.bean.IocBeanId;
import cn.restcloud.framework.core.annotation.rest.MethodType;
import cn.restcloud.framework.core.annotation.rest.Param;
import cn.restcloud.framework.core.annotation.rest.Params;
import cn.restcloud.framework.core.annotation.rest.BodyDataType;
import cn.restcloud.framework.core.annotation.rest.RestConfig;
import cn.restcloud.framework.core.annotation.rest.RestRequestBody;
import cn.restcloud.framework.core.base.IBasePageBean;
import cn.restcloud.framework.core.util.DocumentUtil;
import cn.restcloud.framework.core.util.PrintUtil;
import cn.restcloud.framework.core.util.RequestUtil;
import cn.restcloud.framework.view.base.DocAndView;
import cn.restcloud.framework.view.util.ViewUtil;
/**
 * Controller类发布Rest服务
 * @author Administrator 
 *
 */
@BeanConfig(appId = "example",beanId = "DemoUserRest",beanName = "Rest示例服务",beanType = BeanType.CONTROLLER,autowired = true,singleton = true,hotMode = true)
@RestConfig(url = "/rest/example/users")
public class DemoUserRest {
	
	//自动根据接口路径查找实例类并注入
	private IDemoUserDao dao;
	
	@RestConfig(configName = "根据userId获取用户记录",method = MethodType.GET,url = "/{userid}")
	@Params({ 
		@Param(id = "userid", name = "用户id",required=true),
	})
	public DocAndView getByUserId(@GetParam("userid")String userId) throws Exception{
		UserModel userObj=dao.getByUserId(userId);
		return ViewUtil.getJsonView(userObj);
	}
	
	@RestConfig(configName = "根据id获取用户记录",configId="34234",method = MethodType.GET,url = "/ids/{id}")
	@Params({
		@Param(id = "id", name = "唯一id",required=true),
	})
	public DocAndView getById() throws Exception{
		String id=RequestUtil.getString("id");
		UserModel userObj=dao.getById(id);
		
		return ViewUtil.getJsonView(userObj);
	}
	
	@RestConfig(configName = "列出所有用户记录",method = MethodType.GET,url = "/listAll")
	public DocAndView listAllUsers() throws Exception{
		List<UserModel> users=dao.listAll();
		return ViewUtil.getJsonView(users);
	}
	
	@RestConfig(configName = "分页查询用户数据",method = MethodType.GET,url = "/list")
	@Params({
		@Param(id = "PageNo", name = "当前页兼容page"),
		@Param(id = "pageSize", name = "每页显示数兼容rows"),
		@Param(id = "sort", name = "动态排序字段"),
		@Param(id = "order", name = "排序方向ASC|DESC"),
		@Param(id = "filters", name = "动态过滤条件",sampleValue="{\"fieldName\":\"value\"}"),
		@Param(id = "searchFilters", name = "动态搜索条件",sampleValue="{\"title\":\"test\",\"content\":\"test\"}"),
	})
	public DocAndView listByPage() throws Exception{
		IBasePageBean<UserModel> pageBean=dao.listByPage();
		return ViewUtil.getJsonView(pageBean);
	}
	
	/**
	 * 通过提交json数据来保存用户数据格式:{"userId":"test","userName":"test01"}
	 * @param userModel
	 * @return
	 * @throws Exception
	 */
	@RestRequestBody(type=BodyDataType.JSON)
	@RestConfig(configName = "通过RequestBody请求JSON保存用户数据",method = MethodType.POST,url = "/saveBody")
	public DocAndView saveBody(UserModel userModel) throws Exception{
		int i=dao.save(userModel); //userModel是在参数中直接指定转换的
		return ViewUtil.getSuccessView(String.valueOf(i));
	}
	
	@RestRequestBody(type=BodyDataType.JSON)
	@RestConfig(configName = "通过RequestBody请求JSON保存数据",method = MethodType.POST,url = "/saveBody/doc")
	public DocAndView body2Document(Document doc) throws Exception{
		PrintUtil.o("接收到的数据包为:"+doc); //request body可以直接转换为一个document对像
		return ViewUtil.getJsonView(doc);
	}
	
	/**
	 * 通过RequestBody的json数据转换为Java Bean对像的多种方式1
	 * @param userModel
	 * @return
	 * @throws Exception
	 */
	@RestRequestBody(type=BodyDataType.JSON,cls=UserModel.class)
	@RestConfig(configName = "通过RequestBody请求JSON保存用户数据",method = MethodType.POST,url = "/saveBody/type1")
	public UserModel reqeustBodyJavaBean(UserModel userModel) throws Exception{
		//直接在方法中指定java bean对像，只支持一个Java bean参数
//		PrintUtil.o("userId="+userModel.getUserId());
		userModel.setUserName("test admin");
		return userModel; //直接返回一个java bean对像，在前端接收到的是一个json字符串
	}
	
	/**
	 * 通过RequestBody的json数据转换为Java Bean对像的多种方式2
	 * @param userModel
	 * @return
	 * @throws Exception
	 */
	@RestRequestBody(type=BodyDataType.JSON,cls=UserModel.class)
	@RestConfig(configName = "通过RequestBody请求JSON保存用户数据",method = MethodType.POST,url = "/saveBody/type2")
	public UserModel reqeustBodyJavaBean2() throws Exception{
		//直接在方法中指定java bean对像，只支持一个Java bean参数
		UserModel userModel=RequestUtil.getBean(UserModel.class); //直接从接收到的参数中转换为一个java bean对像
		PrintUtil.o("userId="+userModel.getUserId());
		userModel.setUserName("test admin");
		return userModel; //直接返回一个java bean对像，在前端接收到的是一个json字符串
	}
	
	/**
	 * 通过提交json数据来保存用户数据格式:[{"userId":"test","userName":"test01"},{"userId":"test","userName":"test01"}]
	 * @param userList
	 * @return
	 * @throws Exception
	 */
	@RestRequestBody(type=BodyDataType.JSONARRY,cls=UserModel.class)
	@RestConfig(configName = "通过RequestBody请求JSON数组一次保存多条用户数据",method = MethodType.POST,url = "/saveAllBody")
	public DocAndView saveAllBody(List<UserModel> userList) throws Exception{
		int i=0;
		for(UserModel userObj:userList){
			dao.save(userObj);
			i++;
		}
		return ViewUtil.getSuccessView(String.valueOf(i));
	}
	
	/**
	 * 根据id删除对像，id多个用逗号分隔
	 * @return
	 * @throws Exception
	 */
	@RestConfig(configName = "删除数据",method = "POST",url = "/delete")
	@Params({ 
		@Param(id = "ids", name = "唯一ids",required=true,tip="ids不能为空"),
	})
	public DocAndView deleteById()throws Exception{
		String ids=RequestUtil.getString("ids");
		int r=dao.deleteByIds(ids);//根据id进行删除
		DocAndView docAndView=ViewUtil.getJsonView();
		docAndView.getDoc().put("number", r); //返回删除成功的数量给前端
		return docAndView;
	}
	
	
}
